package com.example.ymm.modules1.service.impl.sys;

import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.modules1.entity.ms.DEmployee;
import com.example.ymm.modules1.entity.my.sys.SysMenu;
import com.example.ymm.modules1.entity.my.sys.SysRoleMenu;
import com.example.ymm.modules1.entity.my.user.ViewDeptUser;
import com.example.ymm.modules1.mapper.my.sys.SysRoleMenuMapper;
import com.example.ymm.modules1.query.sys.SysRoleMenuQueryCriteria;
import com.example.ymm.modules1.service.dto.DbDeptUserQueryCriteria;
import com.example.ymm.modules1.service.hl.YwUserService;
import com.example.ymm.modules1.service.ms.DDepartmentService;
import com.example.ymm.modules1.service.sys.SysRoleMenuService;
import com.example.ymm.modules1.service.user.DbDeptUserService;
import com.example.ymm.util.QueryHelpPlus;
import com.example.ymm.util.UtilValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
@Slf4j
public class SysRoleMenuServiceImpl extends BaseServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements SysRoleMenuService {

    @Autowired
    DbDeptUserService dbDeptUserService;
    @Autowired
    YwUserService ywUserService;

    public List<SysRoleMenu> queryAll(SysRoleMenuQueryCriteria criteria) {
        QueryWrapper predicate = QueryHelpPlus.getPredicate(SysRoleMenu.class, criteria);
        List<SysRoleMenu> result =baseMapper.selectList(predicate);
        return result;
    }

    @Override
    public void removeByRoleValue(String roleValue) {
        LambdaQueryWrapper<SysRoleMenu> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SysRoleMenu::getRoleValue,roleValue);
        remove(queryWrapper);
    }

    @Override
    public SysRoleMenu getRoleValueAndMenuId(String roleValue, String menuId) {
        SysRoleMenuQueryCriteria criteria=new SysRoleMenuQueryCriteria();
        criteria.setRoleValue(roleValue);
        criteria.setMenuId(menuId);
        List<SysRoleMenu> sysRoleMenus = queryAll(criteria);
        SysRoleMenu roleMenu=null;
        if(UtilValidate.isNotEmpty(sysRoleMenus)){
            roleMenu=sysRoleMenus.get(0);
        }
        return roleMenu;
    }

    @Override
    public List<SysMenu> getMenusByRoleValue(String roleValue) {
        SysRoleMenuQueryCriteria criteria=new SysRoleMenuQueryCriteria();
        criteria.setRoleValue(roleValue);
        String userName = getUserName();
        String deptName="";
        DEmployee userInfo = ywUserService.getUserInfoToSlave2(userName);
        DynamicDataSourceContextHolder.clear();
        DynamicDataSourceContextHolder.push("master");
        if(UtilValidate.isNotEmpty(userInfo)){
            deptName=userInfo.getDname();
        }
        criteria.setUserName(userName);
        criteria.setDeptName(deptName);
        return baseMapper.getMenusByRoleValue(criteria);
    }
}
